#ifndef __H_ACTIVEEVENT_H__
#define __H_ACTIVEEVENT_H__
#include "poller.h"
#include <sys/eventfd.h>
#include <functional>
#include <memory>

class CCReactor;

/*
	example:
	=========================================================================================
	auto p_active_event = CActiveEvent::Attach(CCReactor::Instance());
	if (p_active_event)
	{
		// warn:回调函数 执行 线程 为 cc 所在线程
		auto run_thread_id = std::this_thread::get_id();

		p_active_event->SetSignalHandle([run_thread_id](std::int64_t signal){
			// 校验线程安全
		    assert(run_thread_id == std::this_thread::get_id());

			// 执行任务
			std::cout << signal << std::endl;
			// ...
		});
	}
	-----------------------------------------------------------------------------------------
	p_active_event->signal(9527);
	=========================================================================================
*/
class CActiveEvent : public CPollerObject
{
public:
	static std::unique_ptr<CActiveEvent> Attach(CCReactor* cc);

public:
	explicit CActiveEvent();
	virtual ~CActiveEvent();

public:
	int signal(std::int64_t value = 1);

private:
	int create(void);
	int Init();
	int InputNotify(void);
	int HangupNotify(void);

public:
	inline void SetSignalHandle(std::function<void(std::int64_t)> handler)
	{
		m_signal_handler = handler;
	}
private:
	std::function<void(std::int64_t)> m_signal_handler;
};

#endif
